<style lang="less">
@import '../../../styles/table-common.less';
</style>
<template>
  <div class="search">
    <Card>
      <Row @keydown.enter.native="handleSearch">
        <Form ref="searchForm" :model="searchForm" inline :label-width="70">
          <FormItem label="鉴定标题" prop="title" style="width: 300px;">
            <Input type="text" v-model="searchForm.title" clearable placeholder="请输入鉴定标题" style="width: 200px" />
          </FormItem>
          <FormItem label="鉴定类型" prop="authenticateType" style="width: 300px;">
            <Select v-model="searchForm.authenticateType">
              <Option :value="1">汉服</Option>
              <Option :value="2">JK制服</Option>
              <Option :value="3">Lolita</Option>
            </Select>
          </FormItem>

          <FormItem style="margin-left: -35px" class="br">
            <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
            <Button @click="handleReset">重置</Button>
          </FormItem>
        </Form>
      </Row>
      <!--      <Row class="operation"><Button @click="delAll" icon="md-trash">批量删除</Button></Row>-->
      <Row>
        <Table
            :loading="loading"
            border
            :columns="columns"
            :data="data"
            sortable="custom"
            @on-sort-change="changeSort"
            @on-selection-change="showSelect"
            ref="table"
        ></Table>
      </Row>
      <Row type="flex" justify="end" class="page">
        <Page
            :current="searchForm.pageNumber"
            :total="total"
            :page-size="searchForm.pageSize"
            @on-change="changePage"
            @on-page-size-change="changePageSize"
            :page-size-opts="[10, 20, 50]"
            size="small"
            show-total
            show-elevator
            show-sizer
        ></Page>
      </Row>
    </Card>

    <appraisalComment :data="form" v-model="showUser" @on-submit="getDataList" />

    <appraisalDetail :data="form" v-model="detailShow" @on-submit="getDataList" />
  </div>
</template>

<script>
import { getAppraisalData } from '@/api/new.js';
import appraisalComment from './appraisalComment.vue';
import appraisalDetail from './appraisalDetail.vue';
export default {
  name: 'post-manage',
  components: {
    appraisalComment,
    appraisalDetail
  },
  data() {
    return {
      height: 510,
      showUser: false,
      detailShow: false,
      showType: '0',
      loading: true,
      loadingExport: true,
      modalExportAll: false,
      selectList: [],
      searchKey: '',
      searchForm: {
        title: '',
        pageNumber: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        authenticateType: ''
      },
      form: {},
      columns: [
        {
          type: 'index',
          width: 60,
          align: 'center',
          fixed: 'left'
        },
        {
          title: '鉴定标题',
          key: 'title',
          align: 'center',
          minWidth: 110
        },
        {
          title: '鉴定内容',
          key: 'content',
          minWidth: 180,
          render: (h, params) => {
            return h('div', [
              h('span', {
                style: {
                  display: 'inline-block',
                  width: '100%',
                  overflow: 'hidden',
                  textOverflow: 'ellipsis',
                  whiteSpace: 'nowrap',
                  lineClamp: 2
                },
                domProps: {
                  title: params.row.content
                }
              }, params.row.content)
            ]);
          }
        },
        {
          title: '发起人',
          key: 'username',
          align: 'center',
          minWidth: 125
        },
        {
          title: '发起时间',
          key: 'createTime',
          align: 'center',
          minWidth: 125
        },
        {
          title: '操作',
          key: 'action',
          width: 200,
          align: 'center',
          fixed: 'right',
          render: (h, params) => {
            return h('div', [
              h(
                  'a',
                  {
                    on: {
                      click: () => {
                        this.viewDetail(params.row);
                      }
                    }
                  },
                  '详情'
              ),
              h('Divider', {
                props: {
                  type: 'vertical'
                }
              }),
              h(
                  'a',
                  {
                    on: {
                      click: () => {
                        this.viewComment(params.row);
                      }
                    }
                  },
                  '查看评论'
              ),
            ]);
          }
        }
      ],
      data: [],
      total: 0
    };
  },
  methods: {
    init() {
      this.getDataList();
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
      this.clearSelectAll();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    getDataList() {
      // 多条件搜索用户列表
      this.loading = true;
      getAppraisalData(this.searchForm).then(res => {
        this.loading = false;
        if (res.code === 200) {
          this.data = res.data.records;
          this.total = res.data.total;
          if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
            this.searchForm.pageNumber -= 1;
            this.getDataList();
          }
        } else {
          this.data = [];
          this.total = 0;
          if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
            this.searchForm.pageNumber -= 1;
            this.getDataList();
          }
        }
      });
    },
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    handleReset() {
      this.$refs.searchForm.resetFields();
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      // 重新加载数据
      this.getDataList();
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order == 'normal') {
        this.searchForm.order = '';
      }
      this.getDataList();
    },

    // 查看评论
    viewComment(v) {
      // 转换null为""
      for (let attr in v) {
        if (v[attr] == null) {
          v[attr] = '';
        }
      }
      let str = JSON.stringify(v);
      let data = JSON.parse(str);
      this.form = data;
      this.showUser = true;
    },

    // 查看详情
    viewDetail(v) {
      // 转换null为""
      for (let attr in v) {
        if (v[attr] == null) {
          v[attr] = '';
        }
      }
      let str = JSON.stringify(v);
      let data = JSON.parse(str);
      this.form = data;
      this.title = '详情';
      this.detailShow = true;
      console.log(11)
    },

    showSelect(e) {
      this.selectList = e;
      this.selectList.length = e.length;
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false);
    },
    delAll() {
      if (this.selectList.length <= 0) {
        this.$Message.warning('您还未选择要删除的数据');
        return;
      }
      this.$Modal.confirm({
        title: '确认删除',
        content: '您确认要删除所选的 ' + this.selectList.length + ' 条数据?',
        loading: true,
        onOk: () => {
          let ids = '';
          this.selectList.forEach(function(e) {
            ids += e.id + ',';
          });
          ids = ids.substring(0, ids.length - 1);
          deleteUser({ ids: ids }).then(res => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success('删除成功');
              this.clearSelectAll();
              this.getDataList();
            }
          });
        }
      });
    }
  },
  mounted() {
    // 计算高度
    this.height = Number(document.documentElement.clientHeight - 230);
    this.init();
  }
};
</script>
